/*
* @Author: qingsong-Liu
* @Date:   2019-08-31 13:01:01
* @Last Modified by:   qingsong-Liu
* @Last Modified time: 2019-09-01 14:26:44
*/
require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
var _mm=require('util/mm.js');
var _payment=require('service/payment-service.js');
var templateIndex=require('./index.string');

// page 逻辑部分
var page={
    data:{
        orderNumber : _mm.getUrlParam('orderNumber')
    },
    init:function(){
        this.onLoad();
    },
    onLoad:function(){
        this.loadPaymentInfo();
    },
    // 加载支付信息
    loadPaymentInfo:function(){
        var _this = this,
            paymentHtml = '',
            $pageWrap = $('.page-wrap');
        $pageWrap.html('<div class="loading></div>');
        _payment.getPaymentInfo(this.data.orderNumber,function(res){
            // 渲染HTML
            paymentHtml = _mm.renderHtml(templateIndex,res);
            $pageWrap.html(paymentHtml);
            _this.listenOrderStatus();
        },function(errMsg){
            $pageWrap.html('<p class="err-tip">'+errMsg+'</p>');
        });
    },
    // 监听订单状态
    listenOrderStatus:function(){
        var _this = this;
        this.paymentTimer = window.setInterval(function(){
            _payment.getPaymentStatus(_this.data.orderNumber,function(res){
                if (res) {
                    window.location.href
                        = './result.html?type=payment&orderNumber='+_this.data.orderNumber;
                }
            });
        },5e3);
    }
};

$(function(){
    page.init();
});